package com.android.gpuimage;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.common.base.log.DLog;

import java.util.ArrayList;
import java.util.List;

/**
 * 调整的GPUImageView
 */
public class AdjustGPUImageView extends GPUImageView{

	private Bitmap mDstBitmap;
	
	public AdjustGPUImageView(Context context) {
		super(context);
		e();
	}
	
	public AdjustGPUImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		e();
	}

	private void e() {
		super.setScaleType(GPUImage.ScaleType.MATRIX);
	}

	public Bitmap getCurrentBitmap(){
		return this.getGPUImage().getBitmapWithFilterApplied();
	}
	
	public Bitmap getCurrentBitmap(Bitmap bitmap, GPUImageFilter filter){
		if(filter != null){
			return this.getGPUImage().getExternalBitmapWithFilterApplied(bitmap, filter);
		} else{
			return null;
		}
	}

	public Bitmap getmDstBitmap() {
		return mDstBitmap;
	}

	public void setmDstBitmap(Bitmap mDstBitmap) {
		this.mDstBitmap = mDstBitmap;
	}

	public static int a = 0;
	private boolean mZoomEnable = false;
	private boolean mScrollEnable = true;
	private float d = 100.0f;
	private OnClickListener mOnClickListener;
	private OnLongClickListener mOnLongClickListener;
	private OnTouchListener mOnTouchListener;
	private Matrix h = new Matrix();
	private RectF i;
	private boolean mDoubleTapDisable;
	private List<c> k;
	private List<c> l;
	private int m;
	private b n;
	private PointF mCenterPointF = new PointF();
	private PointF p = new PointF();
	private float q = 0.0f;
	private AdjustGPUImageViewAnimator mAdjustGPUImageViewAnimator;
	private TranslationAnimator s;
	private GestureDetector t = new GestureDetector(getContext(), new MyGestureDetector(this));
	class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
		final AdjustGPUImageView zoonGPUImageView;

		public MyGestureDetector(AdjustGPUImageView AdjustGPUImageView) {
			this.zoonGPUImageView = AdjustGPUImageView;
		}

		@Override
		public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent2, float velocityX, float velocityY) {
			if (a == 0 && ((zoonGPUImageView.mAdjustGPUImageViewAnimator == null || !zoonGPUImageView.mAdjustGPUImageViewAnimator.isRunning()) && zoonGPUImageView.mScrollEnable)) {
				zoonGPUImageView.c(velocityX, velocityY);
			}
			return true;
		}

		@Override
		public void onLongPress(MotionEvent motionEvent) {
			if (zoonGPUImageView.mOnLongClickListener != null) {
				zoonGPUImageView.mOnLongClickListener.onLongClick(zoonGPUImageView);
			}
		}

		@Override
		public boolean onDoubleTap(MotionEvent motionEvent) {
			DLog.d("zoomLog", "onDoubleTap");
			if (!zoonGPUImageView.mDoubleTapDisable && a == 1 && (zoonGPUImageView.mAdjustGPUImageViewAnimator == null || !zoonGPUImageView.mAdjustGPUImageViewAnimator.isRunning())) {
				zoonGPUImageView.b(motionEvent.getX(), motionEvent.getY());
			}
			return true;
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent motionEvent) {
			if (!(zoonGPUImageView.mDoubleTapDisable || zoonGPUImageView.mOnClickListener == null)) {
				zoonGPUImageView.mOnClickListener.onClick(zoonGPUImageView);
			}
			return true;
		}

		@Override
		public boolean onSingleTapUp(MotionEvent motionEvent) {
			if (zoonGPUImageView.mDoubleTapDisable && zoonGPUImageView.mOnClickListener != null) {
				zoonGPUImageView.mOnClickListener.onClick(zoonGPUImageView);
			}
			return true;
		}
	}
	private boolean u = false;

	@SuppressLint("NewApi")
	private class TranslationAnimator extends ValueAnimator implements ValueAnimator.AnimatorUpdateListener {
		final AdjustGPUImageView AdjustGPUImageView;
		private float[] b;

		public TranslationAnimator(AdjustGPUImageView AdjustGPUImageView, float velocityX, float velocityY) {
			this.AdjustGPUImageView = AdjustGPUImageView;
			setFloatValues(new float[]{0.0f, 1.0f});
			setDuration(1000000);
			addUpdateListener(this);
			this.b = new float[]{velocityX, velocityY};
		}

		@Override
		public void onAnimationUpdate(ValueAnimator valueAnimator) {
			boolean c = translate(this.b[0], this.b[1]);
			float[] fArr = this.b;
			fArr[0] = fArr[0] * 0.9f;
			fArr = this.b;
			fArr[1] = fArr[1] * 0.9f;
			// TODO: 2018/8/2  
//            if (!mScrollEnable || com.keyboard.colorcam.widget.mCenterPointF.mZoomEnable.mZoomEnable(0.0f, 0.0f, this.mZoomEnable[0], this.mZoomEnable[1]) < 1.0f) {
			if (!c) {
				valueAnimator.cancel();
			}
			if (mOnMuscleFollowListener != null) {
				mOnMuscleFollowListener.follow(h);
			}
		}
	}

	private class b extends ValueAnimator implements ValueAnimator.AnimatorUpdateListener {
		final AdjustGPUImageView AdjustGPUImageView;
		private float[] b;
		private float[] c;
		private float[] d;

		public b(AdjustGPUImageView AdjustGPUImageView) {
			this.AdjustGPUImageView = AdjustGPUImageView;
		}

		@Override
		public void onAnimationUpdate(ValueAnimator valueAnimator) {
			float floatValue = ((Float) valueAnimator.getAnimatedValue()).floatValue();
			for (int i = 0; i < 4; i++) {
				this.d[i] = this.b[i] + ((this.c[i] - this.b[i]) * floatValue);
			}
			if (i == null) {
				i = new RectF();
			}
			i.set(this.d[0], this.d[1], this.d[2], this.d[3]);
			setCurrentMatrixToRenderer();
		}
	}

	public interface c {
		void a(Matrix matrix);
	}

	private class AdjustGPUImageViewAnimator extends ValueAnimator implements ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {
		final AdjustGPUImageView AdjustGPUImageView;
		private float[] b;
		private float[] c;
		//d由b和c计算得到
		private float[] d;

		public AdjustGPUImageViewAnimator(AdjustGPUImageView AdjustGPUImageView, Matrix matrix, Matrix matrix2) {
			this(AdjustGPUImageView, matrix, matrix2, 200);
		}

		public AdjustGPUImageViewAnimator(AdjustGPUImageView AdjustGPUImageView, Matrix matrix, Matrix matrix2, long duration) {
			this.AdjustGPUImageView =  AdjustGPUImageView;
			this.b = new float[9];
			this.c = new float[9];
			this.d = new float[9];
			setFloatValues(new float[]{0.0f, 1.0f});
			setDuration(duration);
			addUpdateListener(this);
			addListener(this);
			matrix.getValues(this.b);
			matrix2.getValues(this.c);
		}

		@Override
		public void onAnimationUpdate(ValueAnimator valueAnimator) {
			float floatValue = ((Float) valueAnimator.getAnimatedValue()).floatValue();
			for (int i = 0; i < 9; i++) {
				//floatValue range from 0 to 1
				this.d[i] = this.b[i] + ((this.c[i] - this.b[i]) * floatValue);
			}
			Matrix matrix = new Matrix();
			matrix.setValues(this.d);
			Matrix matrix2 = new Matrix();
			//矩阵h的逆矩阵存入matrix2
			h.invert(matrix2);
			matrix2.postConcat(matrix);
			matrix = new Matrix(matrix2);
			h.setValues(this.d);
			d(matrix);
			setCurrentMatrixToRenderer();
		}

		@Override
		public void onAnimationStart(Animator animation) {

		}

		@Override
		public void onAnimationEnd(Animator animation) {
			if (mOnMuscleFollowListener != null) {
				mOnMuscleFollowListener.follow(h);
			}
		}

		@Override
		public void onAnimationCancel(Animator animation) {

		}

		@Override
		public void onAnimationRepeat(Animator animation) {

		}
	}

	public void setZoomEnable(boolean enable) {
		this.mZoomEnable = enable;
	}

	public void setScrollEnable(boolean z) {
		this.mScrollEnable = z;
	}

	@Override
	public void setOnClickListener(OnClickListener onClickListener) {
		this.mOnClickListener = onClickListener;
	}

	@Override
	public void setOnLongClickListener(OnLongClickListener onLongClickListener) {
		this.mOnLongClickListener = onLongClickListener;
	}

	@Override
	public void setOnTouchListener(OnTouchListener onTouchListener) {
		super.setOnTouchListener(onTouchListener);
		this.mOnTouchListener = onTouchListener;
	}

	public Matrix getMatrixCopy(Matrix matrix) {
		if (matrix == null) {
			return new Matrix(this.h);
		}
		matrix.set(this.h);
		return matrix;
	}

	/**
	 * 获得一个矩阵
	 * 这个矩阵能够让图片位于View的中心
	 */
	public Matrix getCenterMatrix(Matrix matrix) {
		if (matrix == null) {
			matrix = new Matrix();
		} else {
			matrix.reset();
		}
		if (isValidSize()) {
			RectF bitmapRect = DataQueueUtil.obtainRectF(0.0f, 0.0f, (float) getImageWidth(), (float) getImageHeight());
			RectF viewRect = DataQueueUtil.obtainRectF(0.0f, 0.0f, (float) getWidth(), (float) getHeight());
			matrix.setRectToRect(bitmapRect, viewRect, Matrix.ScaleToFit.CENTER);
			DataQueueUtil.offerRectF(viewRect);
			DataQueueUtil.offerRectF(bitmapRect);
		}
		return matrix;
	}

	/**
	 * 左乘中心矩阵
	 */
	public Matrix postConcatCenterMatrix(Matrix matrix) {
		Matrix centerMatrix = getCenterMatrix(matrix);
		centerMatrix.postConcat(this.h);
		return centerMatrix;
	}

	/**
	 * 获得一个RectF
	 * 这个RectF能使图片位于view的中心，并应用 {@link #h} 的矩阵变换
	 */
	public RectF getCenterRectF(RectF rectF) {
		if (rectF == null) {
			rectF = new RectF();
		} else {
			rectF.setEmpty();
		}
		if (isValidSize()) {
			Matrix matrix = DataQueueUtil.obtainMatrix();
			postConcatCenterMatrix(matrix);
			rectF.set(0.0f, 0.0f, (float) getImageWidth(), (float) getImageHeight());
			matrix.mapRect(rectF);
			DataQueueUtil.offerMatrix(matrix);
		}
		return rectF;
	}

	public RectF getMask() {
		if (this.i != null) {
			return new RectF(this.i);
		}
		return null;
	}

	public int getPinchMode() {
		return a;
	}

	/**
	 * 控制整个View能够水平滑动
	 * 滑动条件：图片被放大至超过屏幕
	 */
	@Override
	public boolean canScrollHorizontally(int i) {
		DLog.d("zoomLog", "canScrollHorizontally()");
		if (a == 2) {
			return true;
		}
		RectF rectF = getCenterRectF(null);
		if (rectF == null) {
			//不能水平滑动：rectF为null，可能是出于初始化状态
			return false;
		}
		if (rectF.isEmpty()) {
			//不能水平滑动：rectF为empty，可能是出于初始化状态
			return false;
		}
		if (i > 0) {
			//是否能向右滑动
			if (rectF.right <= ((float) getWidth())) {
				//不能水平滑动：图片右侧在屏幕内
				return false;
			}
			return true;
		} else if (rectF.left >= 0.0f) {
			//是否能向左滑动
			//不能水平滑动：图片左侧在屏幕内
			return false;
		} else {
			return true;
		}
	}

	/**
	 * 控制整个View能够竖直滑动
	 * 滑动条件：图片被放大至超过屏幕
	 */
	@Override
	public boolean canScrollVertically(int i) {
		DLog.d("zoomLog", "canScrollVertically()");
		if (a == 2) {
			return true;
		}
		RectF rectF = getCenterRectF(null);
		if (rectF == null) {
			//不能竖直滑动：rectF为null，可能是出于初始化状态
			return false;
		}
		if (rectF.isEmpty()) {
			//不能竖直滑动：rectF为empty，可能是出于初始化状态
			return false;
		}
		if (i > 0) {
			//是否能向下滑动
			if (rectF.bottom <= ((float) getHeight())) {
				//不能竖直滑动：图片底部在屏幕内
				return false;
			}
			return true;
		} else if (rectF.top >= 0.0f) {
			//是否能向上滑动
			//不能竖直滑动：图片顶部在屏幕内
			return false;
		} else {
			return true;
		}
	}

	public void setDoubleTapDisabled(boolean disable) {
		this.mDoubleTapDisable = disable;
	}


	public void a(Matrix matrix, long duration) {
		if (matrix != null) {
			a = 0;
			cancelAllAnimators();
			if (duration <= 0) {
				Matrix matrix2 = new Matrix();
				this.h.invert(matrix2);
				matrix2.postConcat(matrix);
				Matrix matrix3 = new Matrix(matrix2);
				this.h.set(matrix);
				d(matrix3);
				setCurrentMatrixToRenderer();
				return;
			}
			this.mAdjustGPUImageViewAnimator = new AdjustGPUImageViewAnimator(this, this.h, matrix, duration);
			this.mAdjustGPUImageViewAnimator.start();
		}
	}

	/**
	 * 这大概是一个destroy时调用的方法
	 */
	public void onDestroy() {
		Matrix matrix = new Matrix();
		this.h.invert(matrix);
		matrix.postConcat(new Matrix());
		Matrix matrix2 = new Matrix(matrix);
		this.h.reset();
		d(matrix2);
		this.i = null;
		a = 0;
		this.mCenterPointF.set(0.0f, 0.0f);
		this.p.set(0.0f, 0.0f);
		this.q = 0.0f;
		if (this.n != null) {
			this.n.cancel();
			this.n = null;
		}
		cancelAllAnimators();
		setCurrentMatrixToRenderer();
	}

	public void a(c cVar) {
		if (cVar != null) {
			if (this.m == 0) {
				if (this.k == null) {
					this.k = new ArrayList();
				}
				this.k.add(cVar);
				return;
			}
			if (this.l == null) {
				if (this.k != null) {
					this.l = new ArrayList(this.k);
				} else {
					this.l = new ArrayList();
				}
			}
			this.l.add(cVar);
		}
	}

	public void b(c cVar) {
		if (cVar != null) {
			if (this.m != 0) {
				if (this.l == null && this.k != null) {
					this.l = new ArrayList(this.k);
				}
				if (this.l != null) {
					this.l.remove(cVar);
				}
			} else if (this.k != null) {
				this.k.remove(cVar);
			}
		}
	}

	private void d(Matrix matrix) {
		if (this.k != null) {
			this.m++;
			for (c a : this.k) {
				a.a(matrix);
			}
			this.m--;
			if (this.m == 0 && this.l != null) {
				this.k = this.l;
				this.l = null;
			}
		}
	}

	protected float getMaxScale() {
		return this.d;
	}

	public void setMaxScale(float f) {
		this.d = f;
	}

	protected float a(float f) {
		return f < 2.0f ? 2.0f : 1.0f;
	}

//	@Override
//	public void setScaleType(com.android.gpuimage.override.GPUImageView.ScaleType aVar) {
//	}

	private void setCurrentMatrixToRenderer() {
		if (isValidSize()) {
			setImageMatrix(this.h);
		}
	}

	private boolean isValidSize() {
		return getImageWidth() > 0 && getImageHeight() > 0 && getWidth() > 0 && getHeight() > 0;
	}

	@SuppressLint({"ClickableViewAccessibility"})
	@Override
	public boolean onTouchEvent(MotionEvent motionEvent) {
		if (!this.mZoomEnable) {
			return super.onTouchEvent(motionEvent);
		}
		super.onTouchEvent(motionEvent);
		/*if (this.mOnTouchListener != null && this.mOnTouchListener.onTouch(this, motionEvent)) {
			return true;
		}*/
		int action = motionEvent.getAction() & 255;
		if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
			if (mGPUImageOnTouchListener != null) {
				mGPUImageOnTouchListener.onTouchOpUp();
			}
			//单点触摸，UP和CANCEL
			if (a == 2) {
				h();
			}
			a = 0;
			//回调，把这个move过程中总的变换矩阵传递出去
			if (mOnMuscleFollowListener != null) {
				mOnMuscleFollowListener.follow(h);
			}
		} else if (action == MotionEvent.ACTION_POINTER_UP) {
			if (mGPUImageOnTouchListener != null) {
				mGPUImageOnTouchListener.onTouchOpUp();
			}
			//多点触摸，UP
			if (a == 2 && motionEvent.getPointerCount() > 2) {
				if ((motionEvent.getAction() >> 8) == 0) {
					a(motionEvent.getX(1), motionEvent.getY(1), motionEvent.getX(2), motionEvent.getY(2));
				} else if ((motionEvent.getAction() >> 8) == 1) {
					a(motionEvent.getX(0), motionEvent.getY(0), motionEvent.getX(2), motionEvent.getY(2));
				}
				//回调，把这个move过程中总的变换矩阵传递出去
				if (mOnMuscleFollowListener != null) {
					mOnMuscleFollowListener.follow(h);
				}
			}
		} else if (action == MotionEvent.ACTION_DOWN) {
			//单点触摸，DOWN
			if (this.mAdjustGPUImageViewAnimator == null || !this.mAdjustGPUImageViewAnimator.isRunning()) {
				if (mGPUImageOnTouchListener != null) {
					mGPUImageOnTouchListener.onTouchOpDown();
				}
				cancelAllAnimators();
				if (this.mScrollEnable) {
					a = 1;
					if (!this.u) {
						this.mCenterPointF.set(motionEvent.getX(), motionEvent.getY());
					}
				}
			}
		} else if (action == MotionEvent.ACTION_POINTER_DOWN) {
			if (mGPUImageOnTouchListener != null) {
				mGPUImageOnTouchListener.onTouchOpDown();
			}
			//多点触摸，DOWN
			cancelAllAnimators();
			a = 2;
			a(motionEvent.getX(0), motionEvent.getY(0), motionEvent.getX(1), motionEvent.getY(1));
		} else if (action == MotionEvent.ACTION_MOVE && (this.mAdjustGPUImageViewAnimator == null || !this.mAdjustGPUImageViewAnimator.isRunning())) {
			//单点触摸，MOVE
			if (a == 1 && !this.u && this.mScrollEnable) {
				//单点
				translate(motionEvent.getX() - this.mCenterPointF.x, motionEvent.getY() - this.mCenterPointF.y);
				mCenterPointF.set(motionEvent.getX(), motionEvent.getY());
			} else if (a == 2 && motionEvent.getPointerCount() > 1) {
				//多点
				float diagonalLength = DataQueueUtil.getRectFDiagonalLength(motionEvent.getX(0), motionEvent.getY(0), motionEvent.getX(1), motionEvent.getY(1));
				float[] center = DataQueueUtil.getRectFCenter(motionEvent.getX(0), motionEvent.getY(0), motionEvent.getX(1), motionEvent.getY(1));
				mCenterPointF.set(center[0], center[1]);
				scale(this.p, this.q, diagonalLength, mCenterPointF);
			}
		}
		this.t.onTouchEvent(motionEvent);
		return true;
	}

	/**
	 * 平移
	 * @param distanceX
	 * @param distanceY
	 * @return
	 */
	private boolean translate(float distanceX, float distanceY) {
		if (!isValidSize()) {
			return false;
		}
		boolean canMove;
		RectF b = DataQueueUtil.obtainRectF();
		getCenterRectF(b);
		float width = (float) getWidth();
		float height = (float) getHeight();
		/**===== 平移条件 start =====**/
		/**
		 * 目的：为了不让图片左边界或右边界移到view里面
		 */
		if (b.right - b.left < width) {
			distanceX = 0.0f;
		} else if (b.left + distanceX > 0.0f) {
			distanceX = b.left < 0.0f ? -b.left : 0.0f;
		} else if (b.right + distanceX < width) {
			distanceX = b.right > width ? width - b.right : 0.0f;
		}
		/**
		 * 目的：为了不让图片上边界或下边界移到view里面
		 */
		if (b.bottom - b.top < height) {
			distanceY = 0.0f;
		} else if (b.top + distanceY > 0.0f) {
			distanceY = b.top < 0.0f ? -b.top : 0.0f;
		} else if (b.bottom + distanceY < height) {
			distanceY = b.bottom > height ? height - b.bottom : 0.0f;
		}
		/**===== 平移条件 end =====**/
		DataQueueUtil.offerRectF(b);
		Matrix matrix = new Matrix();
		matrix.postTranslate(distanceX, distanceY);
		this.h.postTranslate(distanceX, distanceY);
		d(matrix);
		setCurrentMatrixToRenderer();
		if (distanceX == 0.0f && distanceY == 0.0f) {
			canMove = false;
		} else {
			canMove = true;
		}
		return canMove;
	}

	/**
	 * 保存多点触摸时，前两个点的坐标信息
	 */
	private void a(float x1, float y1, float x2, float y2) {
		this.q = DataQueueUtil.getDiagonalValues(this.h)[0] / DataQueueUtil.getRectFDiagonalLength(x1, y1, x2, y2);
		float[] a = DataQueueUtil.reversePoint(DataQueueUtil.getRectFCenter(x1, y1, x2, y2), this.h);
		this.p.set(a[0], a[1]);
	}

	/**
	 *
	 * @param pointF 缩放中心点
	 * @param f
	 * @param diagonalLength 多点触摸时，前两个点组成的RectF的对角线长度
	 * @param pointF2
	 */
	private void scale(PointF pointF, float f, float diagonalLength, PointF pointF2) {
		if (isValidSize()) {
			float f3 = f * diagonalLength;
			Matrix a = DataQueueUtil.obtainMatrix();
			a.postScale(f3, f3, pointF.x, pointF.y);
			a.postTranslate(pointF2.x - pointF.x, pointF2.y - pointF.y);
			Matrix matrix = new Matrix();
			//
			this.h.invert(matrix);
			matrix.postConcat(a);
			Matrix matrix2 = new Matrix(matrix);
			this.h.set(a);
			DataQueueUtil.offerMatrix(a);
			d(matrix2);
			setCurrentMatrixToRenderer();
		}
	}

	private void b(float f, float f2) {
		float f3 = 0.0f;
		if (isValidSize()) {
			Matrix matrix = DataQueueUtil.obtainMatrix();
			getCenterMatrix(matrix);
			float f4 = DataQueueUtil.getDiagonalValues(matrix)[0];
			float f5 = DataQueueUtil.getDiagonalValues(this.h)[0];
			float f6 = f4 * f5;
			float width = (float) getWidth();
			float height = (float) getHeight();
			float maxScale = getMaxScale();
			f5 = a(f5);
			if (f5 <= maxScale) {
				maxScale = f5;
			}
			if (maxScale >= f4) {
				f4 = maxScale;
			}
			Matrix a2 = DataQueueUtil.obtainMatrix(this.h);
			a2.postScale(f4 / f6, f4 / f6, f, f2);
			a2.postTranslate((width / 2.0f) - f, (height / 2.0f) - f2);
			Matrix a3 = DataQueueUtil.obtainMatrix(matrix);
			a3.postConcat(a2);
			RectF a4 = DataQueueUtil.obtainRectF(0.0f, 0.0f, (float) getImageWidth(), (float) getImageHeight());
			a3.mapRect(a4);
			if (a4.right - a4.left < width) {
				f4 = (width / 2.0f) - ((a4.right + a4.left) / 2.0f);
			} else if (a4.left > 0.0f) {
				f4 = -a4.left;
			} else if (a4.right < width) {
				f4 = width - a4.right;
			} else {
				f4 = 0.0f;
			}
			if (a4.bottom - a4.top < height) {
				f3 = (height / 2.0f) - ((a4.bottom + a4.top) / 2.0f);
			} else if (a4.top > 0.0f) {
				f3 = -a4.top;
			} else if (a4.bottom < height) {
				f3 = height - a4.bottom;
			}
			a2.postTranslate(f4, f3);
			cancelAllAnimators();
			this.mAdjustGPUImageViewAnimator = new AdjustGPUImageViewAnimator(this, this.h, a2);
			this.mAdjustGPUImageViewAnimator.start();
			DataQueueUtil.offerRectF(a4);
			DataQueueUtil.offerMatrix(a3);
			DataQueueUtil.offerMatrix(a2);
			DataQueueUtil.offerMatrix(matrix);
		}
	}

	private void h() {
		int i = 0;
		if (isValidSize()) {
			Matrix a = DataQueueUtil.obtainMatrix();
			postConcatCenterMatrix(a);
			float f = DataQueueUtil.getDiagonalValues(a)[0];
			float f2 = DataQueueUtil.getDiagonalValues(this.h)[0];
			float width = (float) getWidth();
			float height = (float) getHeight();
			float maxScale = getMaxScale();
			if (f > maxScale) {
				f = maxScale / f;
			} else {
				f = 1.0f;
			}
			if (f2 * f < 1.0f) {
				f2 = 1.0f / f2;
			} else {
				f2 = f;
			}
			if (f2 != 1.0f) {
				i = 1;
			}
			Matrix a2 = DataQueueUtil.obtainMatrix(a);
			a2.postScale(f2, f2, this.mCenterPointF.x, this.mCenterPointF.y);
			RectF a3 = DataQueueUtil.obtainRectF(0.0f, 0.0f, (float) getImageWidth(), (float) getImageHeight());
			a2.mapRect(a3);
			if (a3.right - a3.left < width) {
				f = (width / 2.0f) - ((a3.right + a3.left) / 2.0f);
			} else if (a3.left > 0.0f) {
				f = -a3.left;
			} else if (a3.right < width) {
				f = width - a3.right;
			} else {
				f = 0.0f;
			}
			float f3 = a3.bottom - a3.top < height ? (height / 2.0f) - ((a3.bottom + a3.top) / 2.0f) : a3.top > 0.0f ? -a3.top : a3.bottom < height ? height - a3.bottom : 0.0f;
			if (!(f == 0.0f && f3 == 0.0f)) {
				i = 1;
			}
			if (i != 0) {
				Matrix a4 = DataQueueUtil.obtainMatrix(this.h);
				a4.postScale(f2, f2, this.mCenterPointF.x, this.mCenterPointF.y);
				a4.postTranslate(f, f3);
				cancelAllAnimators();
				this.mAdjustGPUImageViewAnimator = new AdjustGPUImageViewAnimator(this, this.h, a4);
				this.mAdjustGPUImageViewAnimator.start();
				DataQueueUtil.offerMatrix(a4);
			}
			DataQueueUtil.offerRectF(a3);
			DataQueueUtil.offerMatrix(a2);
			DataQueueUtil.offerMatrix(a);
		}
	}

	private void c(float velocityX, float velocityY) {
		if (isValidSize()) {
			cancelAllAnimators();
			this.s = new TranslationAnimator(this, velocityX / 60.0f, velocityY / 60.0f);
			this.s.start();
		}
	}

	private void cancelAllAnimators() {
		if (this.mAdjustGPUImageViewAnimator != null) {
			this.mAdjustGPUImageViewAnimator.cancel();
			this.mAdjustGPUImageViewAnimator = null;
		}
		if (this.s != null) {
			this.s.cancel();
			this.s = null;
		}
	}

	public void setMoveLock(boolean z) {
		this.u = z;
	}

	@Override
	public boolean performClick() {
		return super.performClick();
	}

	private OnMuscleFollowListener mOnMuscleFollowListener;
	public void setOnMuscleFollowListener(OnMuscleFollowListener listener) {
		mOnMuscleFollowListener = listener;
	}
	public interface OnMuscleFollowListener {
		/**
		 *
		 * @param matrix 初始状态到当前状态的变换
		 */
		void follow(Matrix matrix);
	}

	private AdjustGPUImageOnTouchListener mGPUImageOnTouchListener;

	public void setGPUImageOnTouchListener(AdjustGPUImageOnTouchListener GPUImageOnTouchListener) {
		mGPUImageOnTouchListener = GPUImageOnTouchListener;
	}

	public interface AdjustGPUImageOnTouchListener {

		void onTouchOpDown();

		void onTouchOpUp();

	}

	@Override
	public void resetImageMatrix() {
		this.h.reset();
		super.resetImageMatrix();
		if (mOnMuscleFollowListener != null) {
			mOnMuscleFollowListener.follow(h);
		}
	}
}
